<?php
namespace Admin\Controller;
use Think\Controller;
class QudaoController extends Controller {



   public function _initialize()  
    {  
		header("Content-Type: text/html; charset=utf-8");
		if(!$_SESSION){
    		$message=D('User')->loginout(2);
			session(null);
			$this->success($message,U('index/index'));
			die;
		}

    }
	/**
	 * 银行员工列表
	 * @return [type] [description]
	 */
	public function index(){

		$rs=D("Qudao")->qudao_list();
		$this->assign('count',$rs['count']);
		$this->assign('list',$rs['list']);// 赋值数据集
		$this->assign('page',$rs['show']);// 赋值分页输出
		$this->display('manage_diqu_qudao');
	}
	/**
	 * 数据删除
	 * @return [type] [description]
	 * @DateTime:    [2018-03-24 12:13:22]
	 * 事务玩一下
	 */
	public function delete(){
		$db_data=M("web_user");
        if($_POST){
                $del=$_POST['id_arr'];
                foreach ($_POST['id_arr'] as $key => $value) {
                    $del_arr=$value.$del_arr;
                    if($_POST['id_arr'][$key+1]){
                        $del_arr=','.$del_arr;
                    }
                }
				$web_user=M('web_user');
				$chat_group=M('chat_group');
				$to_user_group=M('to_user_group');
				$wechat_bind=M('wechat_bind');
				$del_in['y_id']=array('in',$del_arr);
				$to_bind_id['web_id']=$to_del['to_user_group']=array('in',$del_arr);
				$wechat_bind->startTrans();
				$bind_id_arr=M('web_user')->where($to_bind_id)->field('bind_id')->select();
				foreach ($bind_id_arr as $key => $value) {
						$bind_id=$value['bind_id'].',';
				}
				$web_user->startTrans();
				$chat_group->startTrans();
				$to_user_group->startTrans();
                $r1=$web_user->delete($del_arr);
                $r2=$chat_group->where($del_in)->delete();
                $r3=$to_user_group->where($to_del)->delete();
                $r2_temp=$chat_group->where($del_in)->select();
                $r3_temp=$to_user_group->where($to_del)->select();
                $r4=$wechat_bind->delete($bind_id);
                if(count($r2_temp)==0){
                	unset($r2);
                	$r2=true;
                }
                if(count($r3_temp)==0){
                	unset($r3);
                	$r3=true;
                }
                if($r1&&$r2&&$r3){
                	$web_user->commit();
                	$chat_group->commit();
                	$to_user_group->commit();
                	$wechat_bind->commit();
 					$this->success('删除成功',U('Qudao/index'),1);
                }
                else{
                	$web_user->rollback();
                	$chat_group->rollback();
                	$to_user_group->rollback();
                	$wechat_bind->rollback();
					$this->error('删除失败',U('Qudao/index'),1);
                }
        }
	}
	/**
	 * 渠道导入
	 * @return [type] [description]
	 */
	
	public function add_qudao_name(){
			$this->display();
	}

	public function qudao_add(){
		$where['qudao_pro_id']=$_SESSION['user']['u_pro_id'];
		$where['qudao_city_id']=$_SESSION['user']['u_city_id'];
	 	$qudao_list = M('qudao')
	 				  ->where($where)
	 				  ->select();
		
		$wechat_where['wechat_id']=$_SESSION['user']['wechat_id'];
		$wechat=M('wechat')->where($wechat_where)->find();
		$appid=$wechat['appid'];
		$appsecret=$wechat['appsecret'];
		$this ->assign('list',$qudao_list);
		$this->display('add_qudao');
	}
	/**
	 * ajax返回银行二维码链接//需要模型
	 */
	public function ajax_bank(){
			$wechat_where['wechat_id']=$_SESSION['user']['wechat_id'];
			$wechat=M('wechat')->where($wechat_where)->find();
			$appid=$wechat['appid'];
			$appsecret=$wechat['appsecret'];
			$api=new \Com\WechatAuth($appid,$appsecret);
			$sst=$_SESSION['user']['u_id'].',yh';
			$res=$api->textCreat($sst);

			$msg['message']="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$res['ticket'];
			$msg['code']=1;
			$this->ajaxReturn($msg);
	}
}
?>